package com.koron.order.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * @ClassName DateUtils
 * @description 获取今天, 昨天 本周 上周 本月 上月 时间范围
 * @Author zhouj
 * @Date 2022/3/10 9:04
 */
@Slf4j
public class DateUtils {

    //获取今天开始时间 2022-03-10 00:00:00
    public static Date getTodayStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar.getTime();
    }

    //获取今天结束时间 2022-03-10 23:59:59
    public static Date getTodayEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);

        return calendar.getTime();
    }

    //获取昨天开始时间 2022-03-09 00:00:00
    public static Date getPreTodayStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getTodayStart());
        calendar.add(Calendar.DAY_OF_YEAR, -1);

        return calendar.getTime();
    }

    //获取昨天结束时间 2022-03-09 23:59:59
    public static Date getPreTodayEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getTodayEnd());
        calendar.add(Calendar.DAY_OF_YEAR, -1);

        return calendar.getTime();
    }

    //获取本周第一天开始时间 2022-03-07 00:00:00
    public static Date getWeekStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_WEEK, 1);
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar.getTime();
    }

    //获取本周最后一天结束时间 2022-03-13 23:59:59
    public static Date getWeekEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_WEEK, 7);
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);

        return calendar.getTime();
    }

    //获取上周第一天开始时间 2022-02-28 00:00:00
    public static Date getPreWeekStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getWeekStart());
        calendar.add(Calendar.DAY_OF_YEAR, -7);

        return calendar.getTime();
    }

    //获取上周最后一天结束时间 2022-03-06 23:59:59
    public static Date getPreWeekEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getWeekEnd());
        calendar.add(Calendar.DAY_OF_YEAR, -7);

        return calendar.getTime();
    }

    //获取本月第一天开始时间 2022-03-01 00:00:00
    public static Date getMonthStart() {

        return getMonthStart(new Date());
    }

    //获取本月最后一天结束时间 2022-03-31 23:59:59
    public static Date getMonthEnd() {

        return getMonthEnd(new Date());
    }

    //获取本年第一天开始时间 2022-03-01 00:00:00
    public static Date getYearStart() {

        return getYearStart(new Date());
    }

    //获取本年最后一天结束时间 2022-03-31 23:59:59
    public static Date getYearEnd() {

        return getYearEnd(new Date());
    }

    //获取本月第一天开始时间 2022-03-01 00:00:00
    public static Date getMonthStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar.getTime();
    }

    //获取本月最后一天结束时间 2022-03-31 23:59:59
    public static Date getMonthEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);

        return calendar.getTime();
    }

    //获取本年第一天开始时间 2022-01-01 00:00:00
    public static Date getYearStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar.getTime();
    }

    //获取本年最后一天结束时间 2022-12-31 23:59:59
    public static Date getYearEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);

        return calendar.getTime();
    }

    //获取上周第一天开始时间 2022-02-01 00:00:00
    public static Date getPreMonthStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getMonthStart());
        calendar.add(Calendar.MONTH, -1);

        return calendar.getTime();
    }

    //获取上月最后一天结束时间 2022-02-28 23:59:59
    public static Date getPreMonthEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getMonthEnd());
        calendar.add(Calendar.MONTH, -1);

        return calendar.getTime();
    }

    //获取上月最后一天结束时间 2022-02-28 23:59:59
    public static String dateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        return sdf.format(date);
    }

    //传入日期字符串及格式化format 返回日期
    public static Date strToDate(String str, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    //获取上月最后一天结束时间 2022-02-28 23:59:59
    public static String getString(Date date, String dateformat) {

        if (Objects.isNull(date)) {
            date = new Date();
        }

        if (StringUtils.isEmpty(dateformat)) {
            dateformat = "yyyy-MM-dd";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(dateformat);

        return sdf.format(date);
    }

    public static int getDays(Date start, Date end) throws ParseException {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        return (int) ((df.parse(df.format(end)).getTime() - df.parse(df.format(start)).getTime()) / (24 * 60 * 60 * 1000));

    }

}
